<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>排班</span>
      </div>

      <div style="margin-bottom: 10px;font-size: 10px;">
        <el-breadcrumb>
          <el-breadcrumb-item>选择：{{ baseMap.hosname }}</el-breadcrumb-item>
          <el-breadcrumb-item>{{ depname }}</el-breadcrumb-item>
          <el-breadcrumb-item>{{ workDate }}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div style="height: 500px;">
        <el-col :span="5" :xs="24" class="pr5">
          <el-card shadow="never">
            <!-- 部门 -->
            <el-tree
              style="height: 450px; overflow-x: hidden"
              highlight-current
              :data="deptList"
              :props="defaultProps"
              accordion
              @node-click="handleNodeClick"
            >
            </el-tree>
          </el-card>
        </el-col>
        <el-col :span="19" :xs="24" class="pl5">
          <el-card shadow="never">
            <el-row style="width: 100%">
              <!-- 排班日期 分页 -->
              <el-tag
                v-for="(item, index) in bookingScheduleList"
                :key="item.id"
                @click="selectDate(item.workDate, index)"
                :type="index == activeIndex ? '' : 'info'"
                style="height: 60px;margin-right: 5px;margin-right:15px;cursor:pointer;"
              >
                {{ item.workDate }} {{ item.dayOfWeek }}<br />
                {{ item.availableNumber }} / {{ item.reservedNumber }}
              </el-tag>

              <!-- 分页 -->
              <el-pagination
                v-show="total > 0"
                :current-page="currentPage"
                :total="total"
                :page-size="currentSize"
                class="pagination"
                layout="prev, pager, next"
                @current-change="getPage"
              />
            </el-row>
            <el-row style="margin-top: 20px;">
              <!-- 排班日期对应的排班医生 -->
              <el-table
                v-loading="listLoading"
                :data="scheduleList"
                border
                fit
                highlight-current-row
              >
                <el-table-column label="序号" width="60" align="center">
                  <template slot-scope="scope">
                    {{ scope.$index + 1 }}
                  </template>
                </el-table-column>
                <el-table-column label="职称" width="150">
                  <template slot-scope="scope">
                    {{ scope.row.title }} | {{ scope.row.docname }}
                  </template>
                </el-table-column>
                <el-table-column label="号源时间" width="80">
                  <template slot-scope="scope">
                    {{ scope.row.workTime == 0 ? "上午" : "下午" }}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="reservedNumber"
                  label="可预约数"
                  width="80"
                />
                <el-table-column
                  prop="availableNumber"
                  label="剩余预约数"
                  width="100"
                />
                <el-table-column prop="amount" label="挂号费(元)" width="90" />
                <el-table-column prop="skill" label="擅长技能" />
              </el-table>
            </el-row>
          </el-card>
        </el-col>
      </div>
    </el-card>
  </div>
</template>
<script>
import {
  getDeptByHoscode,
  getScheduleRule,
  getScheduleDetail
} from "@/api/hosp";
export default {
  data() {
    return {
      // 遮罩层
      listLoading: true,
      currentPage: 1,
      currentSize: 7,
      total: 0,
      deptList: [], //科室
      defaultProps: {
        children: "children",
        label: "depname"
      },
      hoscode: null,
      activeIndex: 0,
      depcode: null,
      depname: null,
      workDate: null,
      bookingScheduleList: [],
      baseMap: {},
      scheduleList: [] //排班详情
    };
  },
  created() {
    this.hoscode = this.$route.params.hoscode;
    this.workDate = this.getCurDate();
    this.getDeptList();
  },
  methods: {
    // 所有科室
    getDeptList() {
      getDeptByHoscode(this.hoscode).then(response => {
        this.deptList = response.data;
        // 默认选中第一个
        if (this.deptList.length > 0) {
          this.bigdepname = this.deptList[0].depname;
          this.depcode = this.deptList[0].children[0].depcode;
          this.depname = this.deptList[0].children[0].depname;
          this.getPage();
        }
      });
    },
    // 排班规则列表
    getScheduleRuleList() {
      this.listLoading = true;
      getScheduleRule(
        this.currentPage,
        this.currentSize,
        this.hoscode,
        this.depcode
      ).then(response => {
        this.bookingScheduleList = response.data.bookingScheduleRuleList;
        this.baseMap = response.data.baseMap;
        this.total = response.data.total;
        if (this.bookingScheduleList.length > 0) {
          this.scheduleList = response.data.scheduleList;
          // 分页后workDate=null，默认选中第一个
          if (this.workDate == null) {
            this.workDate = this.bookingScheduleList[0].workDate;
          }
          //调用查询排班详情
          this.getDetailSchedule();
        } else {
          this.scheduleList = [];
          this.workDate = this.getCurDate();
        }
        this.listLoading = false;
      });
    },
    //查询排班详情
    getDetailSchedule() {
      getScheduleDetail(this.hoscode, this.depcode, this.workDate).then(
        response => {
          this.scheduleList = response.data;
        }
      );
    },
    getPage(page = 1) {
      this.currentPage = page;
      this.workDate = null;
      this.activeIndex = 0;
      this.getScheduleRuleList();
    },
    handleNodeClick(data) {
      // 科室大类直接返回
      if (data.children != null) return;
      this.depcode = data.depcode;
      this.depname = data.depname;
      this.getPage(1);
    },
    selectDate(workDate, index) {
      this.workDate = workDate;
      this.activeIndex = index;
      //调用查询排班详情
      this.getDetailSchedule();
    },
    getCurDate() {
      var datetime = new Date();
      var year = datetime.getFullYear();
      var month =
        datetime.getMonth() + 1 < 10
          ? "0" + (datetime.getMonth() + 1)
          : datetime.getMonth() + 1;
      var date =
        datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
      return year + "-" + month + "-" + date;
    }
  }
};
</script>
<style>
.el-tree-node.is-current > .el-tree-node__content {
  background-color: #409eff !important;
  color: white;
}

.el-checkbox__input.is-checked + .el-checkbox__label {
  color: black;
}
</style>
